package com.xizi.single;


//线程安全的“懒汉模式”
public class Singleton3 {
    // 将自身实例化对象设置为一个属性，并用static修饰
    private static Singleton3 instance;

    // 构造方法私有化
    private Singleton3() {
    }

    // 静态方法返回该实例，加synchronized关键字实现同步
    //synchronized方法通常效率低，显然这不是最佳的实现方案
    public static synchronized Singleton3 getInstance() {
        if (instance == null) {
            instance = new Singleton3();
        }
        return instance;
    }

    public static void main(String[] args) {
        Singleton3 singleton3 = Singleton3.getInstance();
        Singleton3 singleton4 = Singleton3.getInstance();
        System.out.println(singleton4 == singleton3); //true
    }
}
